package com.pine.dbant.core.web;

import com.pine.dbant.common.plugin.ExecuteRecord;
import com.pine.dbant.common.plugin.QueryExecuteRecord;
import com.pine.dbant.core.model.dto.ExecuteScriptDTO;
import com.pine.dbant.core.service.SqlDatabaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 结构类型的数据库操作
 *
 * @author xiaoyuan
 * @create 2022/11/30 10:11
 **/
@Api(tags = "结构类型的数据库服务")
@RestController
@RequestMapping("/sqlDatabase")
public class SqlDatabaseController extends BaseHandler {

    @Autowired
    public SqlDatabaseService sqlDatabaseService;

//    @ApiOperation("获取某个表信息")
//    @GetMapping("/getTableMeta")
//    public Table getTableMeta(@NotNull(message = "连接id不能为空") Long connId,
//                              @NotBlank(message = "数据库名字不能为空") String catalog,
//                              String schema, @NotBlank(message = "表名字不能为空") String tableName) {
//        return sqlDatabaseService.getTableMeta(connId, catalog, schema, tableName);
//    }


    @ApiOperation("获取某个表数据")
    @GetMapping("/queryDataOneTable")
    public QueryExecuteRecord queryDataOneTable(@NotNull(message = "连接id不能为空") Long connId,
                                                @NotBlank(message = "数据库名字不能为空") String catalog,
                                                String schema, @NotBlank(message = "表名字不能为空") String tableName) {
        return sqlDatabaseService.queryDataOneTable(connId, catalog, schema, tableName);
    }

    @ApiOperation("执行脚本信息")
    @PostMapping("/executeScript")
    public List<ExecuteRecord> executeScript(@Valid @RequestBody ExecuteScriptDTO executeParams) {
        return sqlDatabaseService.executeScript(executeParams.getConnId(), executeParams.getCatalog(),
                executeParams.getSchema(), executeParams.getExecuteParams().toExecuteParams());
    }


    public void saveTable(){

    }

    @ApiOperation("清空表数据")
    @PostMapping("/clearTable")
    public void clearTable(@NotNull(message = "连接id不能为空") Long connId,
                               @NotBlank(message = "数据库名字不能为空") String catalog,
                               String schema, @NotBlank(message = "表名字不能为空") String tableName){

    }

    @ApiOperation("删除表")
    @PostMapping("/deleteTable")
    public void deleteTable(@NotNull(message = "连接id不能为空") Long connId,
                               @NotBlank(message = "数据库名字不能为空") String catalog,
                               String schema, @NotBlank(message = "表名字不能为空") String tableName){

    }

    @ApiOperation("复制表")
    @PostMapping("/copyTable")
    public void copyTable(@NotNull(message = "连接id不能为空") Long connId,
                            @NotBlank(message = "数据库名字不能为空") String catalog,
                            String schema, @NotBlank(message = "表名字不能为空") String tableName){

    }

    @ApiOperation("重命名表名称")
    @PostMapping("/renameTable")
    public void renameTable(@NotNull(message = "连接id不能为空") Long connId,
                          @NotBlank(message = "数据库名字不能为空") String catalog,
                          String schema, @NotBlank(message = "表名字不能为空") String tableName){

    }
}
